﻿using System;
using System.IO;
using System.Text;

namespace Program06._1
{
    class Program
    {
        private static void Main(string[] args)
        {
            ////使用FileStream来写入文件
            //using(FileStream fsWrite=new FileStream("new.txt", FileMode.OpenOrCreate, FileAccess.Write))
            //{
            //    var str = "看我把你覆盖";
            //    //把字符串转换成二进制
            //    byte[] buffer = Encoding.UTF8.GetBytes(str);
            //    fsWrite.Write(buffer, 0, buffer.Length);
            //}
            //Console.WriteLine("把文件写入成功!");

            //读取文件
            FileStream fsRead = new FileStream(@"E:\CsharpAdvanced\Program06.1\bin\Debug\netcoreapp3.1\new.txt",
            FileMode.OpenOrCreate,FileAccess.Read);

            //读取文件大小不超过5M
            byte[] buffer = new byte[1024 * 1024 * 5];

            //读取当前文件的有效字节数
            int r = fsRead.Read(buffer, 0, buffer.Length);

            //读取出的二进制字节流转转换成utf8格式的字符串
            string str = Encoding.UTF8.GetString(buffer, 0, r);
            //关闭文件流
            fsRead.Close();
            //释放存储空间、线程
            fsRead.Dispose();
            Console.WriteLine(str);
            

        }
    }
}
